package com.um.jdy.platform.shop.manager;

import cn.hutool.core.date.DateUtil;
import com.um.jdy.common.commons.properties.HshChargingProperties;
import com.um.jdy.common.platform.entity.po.AppPackage;
import com.um.jdy.common.platform.service.AppPackageService;
import com.um.jdy.common.shop.service.ShopOrderService;
import com.um.jdy.platform.order.entity.vo.OverviewOrderItemVO;
import com.um.jdy.platform.shop.entity.vo.ShopOverviewItemVO;
import com.um.jdy.platform.shop.entity.vo.ShopOverviewVO;
import com.um.springboot.starter.orm.manager.BaseManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;


@Component
@Slf4j
public class ShopOverviewManager extends BaseManager<ShopOrderService> {

    @Autowired
    HshChargingProperties hshChargingProperties;

    @Autowired
    AppPackageService appPackageService;

    /**
     * 订单概览图表
     * @return
     */
    public ShopOverviewVO getOverviewData(){

        Date date = new Date();
        String comId = getComId();
        String year = DateUtil.format(date,"yyyy");

        ShopOverviewVO shopOverviewVO = new ShopOverviewVO();

        shopOverviewVO.setMallLimitDate(hshChargingProperties.isLimitDeviceDate());
        if(hshChargingProperties.isLimitDeviceDate()){
            AppPackage appPackage = appPackageService.getEntity("com_id",getComId());
            if(appPackage != null && appPackage.getMallTime() != null)
                shopOverviewVO.setDays(com.um.springboot.starter.utils.DateUtil.daysBetween(new Date(),appPackage.getMallTime()));
        }

        //今天时间
        Date today = new Date();
        String todayBeginDate = DateUtil.format(today,"yyyy-MM-dd")+" 00:00:00";
        String todayEndDate = DateUtil.format(today,"yyyy-MM-dd")+" 23:59:59";

        OverviewOrderItemVO todayOrderGmv = service.getViewModel("get_shop_order_overview_gmv",OverviewOrderItemVO.class,new Object[]{comId,todayBeginDate,todayEndDate});
        OverviewOrderItemVO todayOrder = service.getViewModel("get_shop_order_overview_valid",OverviewOrderItemVO.class,new Object[]{comId,todayBeginDate,todayEndDate});
        Long todayActiveUsers = service.getCountBySqlName("get_active_users",new Object[]{comId,todayBeginDate,todayEndDate});

        //昨天时间
        Date yestoday = new Date();
        yestoday.setDate(today.getDate() - 1);
        String yestodayBeginDate = DateUtil.format(yestoday,"yyyy-MM-dd")+" 00:00:00";
        String yestodayEndDate = DateUtil.format(yestoday,"yyyy-MM-dd")+" 23:59:59";

        OverviewOrderItemVO yestodayOrderGmv = service.getViewModel("get_shop_order_overview_gmv",OverviewOrderItemVO.class,new Object[]{comId,yestodayBeginDate,yestodayEndDate});
        OverviewOrderItemVO yestodayOrder = service.getViewModel("get_shop_order_overview_valid",OverviewOrderItemVO.class,new Object[]{comId,yestodayBeginDate,yestodayEndDate});
        Long yestodayActiveUsers = service.getCountBySqlName("get_active_users",new Object[]{comId,yestodayBeginDate,yestodayEndDate});

        //本月
        Date month = new Date();
        String monthBeginDate = DateUtil.beginOfMonth(month).toString();
        String monthEndDate = DateUtil.endOfMonth(month).toString();

        OverviewOrderItemVO monthOrderGmv = service.getViewModel("get_shop_order_overview_gmv",OverviewOrderItemVO.class,new Object[]{comId,monthBeginDate,monthEndDate});
        OverviewOrderItemVO monthOrder = service.getViewModel("get_shop_order_overview_valid",OverviewOrderItemVO.class,new Object[]{comId,monthBeginDate,monthEndDate});
        Long monthActiveUsers = service.getCountBySqlName("get_active_users",new Object[]{comId,monthBeginDate,monthEndDate});

        //上月
        Date last = new Date();
        Calendar calendar = Calendar.getInstance();
        // 设置为当前时间
        calendar.setTime(last);
        calendar.add(Calendar.MONTH,-1);
        last = calendar.getTime();

        String lastBeginDate = DateUtil.beginOfMonth(last).toString();
        String lastEndDate = DateUtil.endOfMonth(last).toString();

        OverviewOrderItemVO lastOrderGmv = service.getViewModel("get_shop_order_overview_gmv",OverviewOrderItemVO.class,new Object[]{comId,lastBeginDate,lastEndDate});
        OverviewOrderItemVO lastOrder = service.getViewModel("get_shop_order_overview_valid",OverviewOrderItemVO.class,new Object[]{comId,lastBeginDate,lastEndDate});
        Long lastActiveUsers = service.getCountBySqlName("get_active_users",new Object[]{comId,lastBeginDate,lastEndDate});

        shopOverviewVO.setTodayGmv(todayOrderGmv.getAmount());
        shopOverviewVO.setTodayNum(todayOrder.getNum());
        shopOverviewVO.setTodayPrice(todayOrder.getAmount());
        shopOverviewVO.setTodayActiveUsers(todayActiveUsers);

        shopOverviewVO.setYestodayGmv(yestodayOrderGmv.getAmount());
        shopOverviewVO.setYestodayNum(yestodayOrder.getNum());
        shopOverviewVO.setYestodayPrice(yestodayOrder.getAmount());
        shopOverviewVO.setYestodayActiveUsers(yestodayActiveUsers);

        shopOverviewVO.setDayDifferGmv(todayOrderGmv.getAmount() - yestodayOrderGmv.getAmount());
        shopOverviewVO.setDayDifferNum(todayOrder.getNum() - yestodayOrder.getNum());
        shopOverviewVO.setDayDifferPrice(todayOrder.getAmount() - yestodayOrder.getAmount());
        shopOverviewVO.setDayDifferUsers(todayActiveUsers - yestodayActiveUsers);

        shopOverviewVO.setMonthGmv(monthOrderGmv.getAmount());
        shopOverviewVO.setMonthNum(monthOrder.getNum());
        shopOverviewVO.setMonthPrice(monthOrder.getAmount());
        shopOverviewVO.setMonthActiveUsers(monthActiveUsers);

        shopOverviewVO.setLastGmv(lastOrderGmv.getAmount());
        shopOverviewVO.setLastNum(lastOrder.getNum());
        shopOverviewVO.setLastPrice(lastOrder.getAmount());
        shopOverviewVO.setLastActiveUsers(lastActiveUsers);

        shopOverviewVO.setMonthDifferGmv(monthOrderGmv.getAmount() - lastOrderGmv.getAmount());
        shopOverviewVO.setMonthDifferNum(monthOrder.getNum() - lastOrder.getNum());
        shopOverviewVO.setMonthDifferPrice(monthOrder.getAmount() - lastOrder.getAmount());
        shopOverviewVO.setMonthDifferUsers(monthActiveUsers - lastActiveUsers);


        Long tosendOrders = service.getCountBySqlName("get_tosend_orders",new Object[]{comId});
        Long aftersaleOrders = service.getCountBySqlName("get_aftersale_orders",new Object[]{comId});

        shopOverviewVO.setTosendOrders(tosendOrders);
        shopOverviewVO.setAftersaleOrders(aftersaleOrders);

        Long upNum = service.getCountBySqlName("get_goods_up_num",new Object[]{comId});
        Long downNum = service.getCountBySqlName("get_goods_down_num",new Object[]{comId});
        Long lowStockNum = service.getCountBySqlName("get_goods_low_stock_num",new Object[]{comId});
        Long zeroNum = service.getCountBySqlName("get_goods_zero_num",new Object[]{comId});
        Long totalNum = service.getCountBySqlName("get_goods_total_num",new Object[]{comId});

        shopOverviewVO.setGoodsUpNum(upNum);
        shopOverviewVO.setGoodsDownNum(downNum);
        shopOverviewVO.setGoodsLowStock(lowStockNum);
        shopOverviewVO.setGoodsZeroStockNum(zeroNum);
        shopOverviewVO.setGoodsTotalNum(totalNum);

        return shopOverviewVO;
    }



    public List<ShopOverviewItemVO> getSaleCharts(String dateType){

        String beginDateTime = "";
        String endDateTime = "";

        List<String> keys = new ArrayList();
        String sqlName = "";
        if(dateType.equals("0")){

            sqlName = "get_hour_order_list";
            beginDateTime = DateUtil.format(new Date(),"yyyy-MM-dd")+" 00:00:00";
            endDateTime = DateUtil.format(new Date(),"yyyy-MM-dd")+" 23:59:59";

            for(int index = 0;index < 24; index ++){
                keys.add(index+"");
            }
        }else if(dateType.equals("1")){
            sqlName = "get_day_order_list";
            Date beginTime = new Date();
            Date endTime = new Date();
            beginTime.setDate(beginTime.getDate() - 7);
            beginDateTime = DateUtil.format(beginTime,"yyyy-MM-dd")+" 00:00:00";
            endDateTime = DateUtil.format(endTime,"yyyy-MM-dd")+" 23:59:59";

            while(beginTime.getTime() <= endTime.getTime()){
                keys.add(DateUtil.format(beginTime,"MM-dd"));
                beginTime.setDate(beginTime.getDate() + 1);
            }
        }else{
            sqlName = "get_day_order_list";
            Date beginTime = new Date();
            Date endTime = new Date();
            beginTime.setDate(beginTime.getDate() - 30);
            beginDateTime = DateUtil.format(beginTime,"yyyy-MM-dd")+" 00:00:00";
            endDateTime = DateUtil.format(endTime,"yyyy-MM-dd")+" 23:59:59";

            while(beginTime.getTime() <= endTime.getTime()){
                keys.add(DateUtil.format(beginTime,"MM-dd"));
                beginTime.setDate(beginTime.getDate() + 1);
            }
        }


        List<ShopOverviewItemVO> listData = service.getViewModelList(sqlName,ShopOverviewItemVO.class,new Object[]{getComId(),beginDateTime,endDateTime});



        List<ShopOverviewItemVO> list = new ArrayList<>();

        for(int keyIndex = 0;keyIndex < keys.size();keyIndex ++){
            String key = keys.get(keyIndex);

            ShopOverviewItemVO item = new ShopOverviewItemVO();
            item.setTime(key);

            for(ShopOverviewItemVO dataItem : listData){
                if(key.equals(dataItem.getTime())){
                    item.setNum(dataItem.getNum());
                    item.setPayment(dataItem.getPayment());
                    break;
                }
            }

            list.add(item);
        }

        return list;
    }
}
